package com.ys.controller.web.base;

import com.ys.config.MenuId;
import com.ys.entity.base.TbUserPaySlip;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工工资条信息表
 */
@Controller
@RequestMapping("/web/base/tbUserPaySlip")
public class WebTbUserPaySlipController {
	private static final Logger logger = LoggerFactory.getLogger(WebTbUserPaySlipController.class);

	@Autowired
	private CommonApiService<TbUserPaySlip> commonApiService;


    public Map<String, Object> getEntity(Long id){
        Map<String,Object> entity = new TbUserPaySlip()
                .addLeftJoin("TS_UserInfo U ON U.UserId = A.UserId")//用户信息表
                .addField("(U.UserId + '/' + U.UserName) AS _ys_nameLinked")//工号/姓名

                .addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
                .addField("OI.DeptName AS deptName")//部门

                .addLeftJoin("THR_UserArchives UAS ON UAS.UserId = A.UserId")//用户档案表
                .addField("UAS.UserState AS userState")//员工状态

                .addLeftJoin("TS_OrganizationInfo O ON O.OrgId = A.OrgId")//组织机构表
                .addField("O.OrgName AS orgName")//所属公司

            .addLeftJoin("TB_UserMonthPerformance MP ON MP.Id = A.MonthPerformanceId")//员工月度绩效信息表
                .addField("MP.Rank AS rank")//岗位职级
                .addField("MP.Type AS type")//岗位工种(0总经理 1副总经理 2责任月薪型员工 3绩效提奖型员工 4研发人员)
                .addField("MP.JobBonus AS jobBonus")//岗位提奖/月

            .addLeftJoin("TB_JobLevel JB ON JB.Id = A.RoleLevelId")//岗位职级管理表
                .addField("JB.Category AS category")//岗位类别
                .addField("JB.Categories AS categories")//职级大类(0A 1B 2C 3D 4P)
                .addField("JB.FixsalaryStandard AS fixsalaryStandard")//定薪标准
                .addField("JB.SalaryStandard AS salaryStandard")//发薪标准
                .addField("JB.BasicSalary AS basicSalary")//基本底薪
                .addField("JB.PositionSalary AS positionSalary")//岗位薪资
                .addField("JB.TransSubsidy AS transSubsidy")//交通补贴
                .addField("JB.CateringSubsidy AS cateringSubsidy")//餐饮补贴


            .setId(id)
            .queryFirstMap();

        return entity;
    }


    public Map<String, Object> setExtraParam(Map<String,Object> entity){
        Map<String, Object> extraParam = new HashMap<String, Object>();
        //关联数据
        if(entity.get("_ys_nameLinked")!=null)extraParam.put("_ys_nameLinked", entity.get("_ys_nameLinked"));
        if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
        if(entity.get("userState")!=null)extraParam.put("userState", entity.get("userState"));
        if(entity.get("orgName")!=null)extraParam.put("orgName", entity.get("orgName"));

        if(entity.get("rank")!=null)extraParam.put("rank", entity.get("rank"));
        if(entity.get("type")!=null)extraParam.put("type", entity.get("type"));
        if(entity.get("category")!=null)extraParam.put("category", entity.get("category"));
        if(entity.get("categories")!=null)extraParam.put("categories", entity.get("categories"));
        if(entity.get("fixsalaryStandard")!=null)extraParam.put("fixsalaryStandard", entity.get("fixsalaryStandard"));
        if(entity.get("salaryStandard")!=null)extraParam.put("salaryStandard", entity.get("salaryStandard"));
        if(entity.get("basicSalary")!=null)extraParam.put("basicSalary", entity.get("basicSalary"));
        if(entity.get("positionSalary")!=null)extraParam.put("positionSalary", entity.get("positionSalary"));
        if(entity.get("transSubsidy")!=null)extraParam.put("transSubsidy", entity.get("transSubsidy"));
        if(entity.get("cateringSubsidy")!=null)extraParam.put("cateringSubsidy", entity.get("cateringSubsidy"));
        if(entity.get("jobBonus")!=null)extraParam.put("jobBonus", entity.get("jobBonus"));
        if(entity.get("attendancePerformance")!=null)extraParam.put("attendancePerformance", new BigDecimal(entity.get("attendancePerformance").toString()).negate());

        return extraParam;
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String,Object> entity = getEntity(id);
            Map<String, Object> extraParam = setExtraParam(entity);
            if(entity.get("slipStatus")!=null){
                String slipStatus = entity.get("slipStatus").toString();
                if(slipStatus.equals("0")){//工资状态为“空”才可编辑
                    model.addAttribute("onProcess", 0);
                    commonApiService.toEdit(model,id, new TbUserPaySlip(), MenuId.TB_UserPaySlip, extraParam, null);
                    return "base/tbUserPaySlip/toEdit";
                }
            }
            model.addAttribute("onProcess", 1);
            commonApiService.toDetail(model,id, new TbUserPaySlip(), MenuId.TB_UserPaySlip, extraParam, null);
            return "base/tbUserPaySlip/toEdit";

        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String,Object> entity = getEntity(id);
            Map<String, Object> extraParam = setExtraParam(entity);
            model.addAttribute("onProcess", 1);
            return commonApiService.toDetail(model,id, new TbUserPaySlip(), MenuId.TB_UserPaySlip, extraParam, null);
            //return "base/tbUserPaySlip/toEdit";
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }




}






